package cn.kgc.controller;

import cn.kgc.entity.Emp;
import cn.kgc.service.EmpService;
import cn.kgc.va.EmpSearch;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Controller
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;
    @GetMapping("/emp")
    public String getAllEmp(Model model){
        ExecutorService pool = Executors.newFixedThreadPool(1000);
        for (int i=0;i<2000;i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    empService.getAllEmp();
                }
            });
        }
        List<Emp> list = empService.getAllEmp();
        model.addAttribute("emplist",list);
        return "emplist";

    }
    @GetMapping("/emp/{id}")
    public String getEmpById(@PathVariable("id")Integer id,Model model){
        Emp emp =empService.getEmpById(id);
        model.addAttribute("emp",emp);
        Map<String,Object> sexs= new HashMap<>();
        sexs.put("M","男");
        sexs.put("W","女");
        sexs.put("O","妖");
        model.addAttribute("sexs",sexs);
        return "emp";
    }
    @PostMapping("/emp")
    public String addEmp(Emp emp){
        empService.addEmp(emp);
        return "redirect:/emp";
    }
    @PutMapping("/emp")
    public String updateEmp(Emp emp){
        System.out.println("1");
        empService.updateEmp(emp);
        return "redirect:/emp";
    }
    @DeleteMapping("/emp/{id}")
    public String deleteEmpById(@PathVariable("id")Integer id){
        System.out.println("1");
        empService.deleteEmpById(id);
        return "redirect:/emp";
    }
    @GetMapping("/emp/view")
    public String toAdvice(Model model){
        Map<String,Object> sexs= new HashMap<>();
        sexs.put("M","男");
        sexs.put("W","女");
        sexs.put("O","妖");
        model.addAttribute("sexs",sexs);
        model.addAttribute("emp", new Emp());
        return "emp";
    }
    @PostMapping("/emp/search")
    public String searchEmp(EmpSearch empSearch,Model model){
        List<Emp> list = empService.searchEmp(empSearch);
        model.addAttribute("emplist",list);
        log.debug("debug:empList:"+list);
        log.info("info:emplist:"+list);
        return "emplist";
    }
    @ResponseBody
    @GetMapping("/set/{key}/{value}")
    public String redisSet(@PathVariable("key")String key,@PathVariable("value")String value){
        redisTemplate.opsForValue().set(key,value);
        return "set ok";
    }
    @ResponseBody
    @GetMapping("/set/{key}")
    public String redisGet(@PathVariable("key")String key){
        String value=(String)redisTemplate.opsForValue().get(key);
        return value;
    }
}
